/**
 * 
 */
package org.jms.examples.activemqexamples.p2p;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

/**
 * @author sudrsan
 *
 */
public class Consumer {

	private ConnectionFactory connectionFactory = null;
	private Connection connection = null;
	private Destination destination = null;
	private MessageConsumer consumer = null;
	private Session session = null;

	public void readMessage() {
		connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
		try {
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue("SAMPLEQUEUE");
			consumer = session.createConsumer(destination);

			Message message = consumer.receive();

			if (message instanceof TextMessage) {
				System.out.println("The Message is : "
						+ ((TextMessage) message).getText());
			}
		} catch (JMSException e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Consumer consumer = new Consumer();
		consumer.readMessage();
	}

}
